package com.orangelife.mobile.individual.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import com.curry.android.http.HttpUtil;
import com.curry.android.util.DecimalUtil;
import com.curry.android.util.DialogHelper;
import com.curry.android.util.IntentHelper;
import com.curry.android.util.JSONHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.app.application.CurryApplication;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.util.ToastHelper;

import java.util.Map;

import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyWalletActivity extends OrangeLifeBaseActivity  {

    private TextView tvTitle, tvAvailableValue, tvFreezeBalanceValue;
    private Dialog dlgLoading;
    private String walletID;
    private String totalMoney;
    /** 总金额 */
    private String money;
    /** 可用金额 */
    private String frozenMoney;
    /** 冻结金额 */
    private static final int WALLET_BALANCE = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_individual_my_wallet2);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        ButterKnife.bind(this);
        getDataFromServer();
        closeActivity();
    }

    private void getDataFromServer() {
        dlgLoading = DialogHelper.getInstance().createLoadingDialog(this,
                Constant.LOADING);
        dlgLoading.show();
        String url = Constant.URL_MY_WALLET;
        JSONRequest.getInstance().sendRequest(JSONRequest.GET, url, null, handler, WALLET_BALANCE, HttpUtil.ACTION_URI);
    }

    private void initView() {

//		rlBalanceWithdraw = (RelativeLayout)findViewById(R.id.rlBalanceWithdraw);
        tvAvailableValue = (TextView) findViewById(R.id.tvAvailableValue);
        tvFreezeBalanceValue = (TextView) findViewById(R.id.tvFreezeBalanceValue);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText(getResources().getString(R.string.my_wallet));
    }


    @OnClick({R.id.tvBalanceDetail,R.id.tvAvailableValue,R.id.btnRecharge,R.id.tvFreezeBalanceValue,R.id.rlBalanceRecharge})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tvBalanceDetail:/*收支明细*/
                if(walletID!=null) {
                    IntentHelper.getInstances().intentWithExtra(this, BalanceDetailActivity.class, "walletID", walletID);
                }
                break;
            case R.id.tvAvailableValue: /*账户可用余额*/
                break;
            case R.id.tvFreezeBalanceValue:/*账户冻结金额*/
                IntentHelper.getInstances().getIntent(this, FreezeExplainActivity.class);
                break;
            case R.id.rlBalanceRecharge:/*支付密码*/
                IntentHelper.getInstances().getIntent(this, PayPwdActivity.class);
                break;
            case R.id.btnRecharge:/*充值*/
                IntentHelper.getInstances().getIntent(this, BalanceRechargeActivity.class);
                break;
            default:
                break;
        }
    }

    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 500:
                    ToastHelper.getInstance()._toast(msg.obj.toString());
                    DialogHelper.closeDialog(dlgLoading);
                    break;
                case WALLET_BALANCE:
                    DialogHelper.closeDialog(dlgLoading);
                    String json = (String) msg.obj;
                    Map<String, Object> map = JSONHelper.jsonToMap(json);
                    if (map != null) {
                        Map<String, Object> mapList = (Map<String, Object>) map.get(
                                "entity");
                        setTextValue(mapList);
                    }
                    break;
                case 1011:
                    isLogin(dlgLoading);
                    break;
                default:
                    break;
            }
        }
    };

    private void setTextValue(Map<String, Object> map) {
        if (map != null) {
            walletID = String.valueOf(map.get("walletID"));
            money = String.valueOf(map.get("money"));
            totalMoney = String.valueOf(map.get("totalMoney"));
            frozenMoney = String.valueOf(map.get("frozenMoney"));
            tvAvailableValue.setText(DecimalUtil.decimalFormat(money));
            tvFreezeBalanceValue.setText(DecimalUtil.decimalFormat(frozenMoney));
            CurryApplication.getInstance().setWalletID(walletID);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
